package com.io;

import java.io.*;

/**
 * @author: 一个小菜逼
 * @Date: 2020/4/28
 */
public class MyFileIo {

    public static void main(String[] args) {
        String src="D:\\test\\testFile01.txt";
        String des="D:\\test\\copytestFile01.txt";
        fileCopy(src,des);
    }

    public static void fileCopy(String src, String des) {
        File srcFile = new File(src);
        File desFile = new File(des);
        FileInputStream fileInputStream=null;
        FileOutputStream fileOutputStream=null;
        byte[] bytesBuffer = new byte[1024 * 1024];
        int len=0;
        try {
            //如果该srcFile不存在，则会直接出现异常
            fileInputStream = new FileInputStream(srcFile);
            //如果该desFile不存在，则会创建文件
            fileOutputStream = new FileOutputStream(desFile);
            while ((len=fileInputStream.read(bytesBuffer))!=-1){
                fileOutputStream.write(bytesBuffer,0,len);
            }
        }catch (IOException exception){
            exception.printStackTrace();
        }finally {
            try {
                fileOutputStream.close();
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
